package problem1419;

//1419.数青蛙
//https://leetcode.cn/problems/minimum-number-of-frogs-croaking/

import java.util.HashMap;

class Solution {
    public int minNumberOfFrogs(String croakOfFrogs) {
        char[] s = croakOfFrogs.toCharArray();
        String t = "croak";
        int n = t.length();
        int[] hash = new int[n];
        HashMap<Character, Integer> index = new HashMap<>();
        for(int i = 0; i<n; i++) {
            index.put(t.charAt(i), i);
        }

        for(int i = 0; i<s.length; i++) {
            int cur = index.get(s[i]);
            System.out.print(cur + " ");
            if(cur == 0) {
                if(hash[n-1] > 0) hash[n-1]--;
                hash[cur]++;
            }else {
                if(hash[cur-1] == 0) return -1;
                hash[cur-1]--;
                hash[cur]++;
            }
        }

        for(int i = 0; i<n-1; i++) {
            if(hash[i] != 0) return -1;
        }
        return hash[n-1];
    }
}

/**

class Solution {
    public int minNumberOfFrogs(String str) {
        int n = str.length();
        char[] s = "croak".toCharArray();
        int[] hash = new int[127];
        for(int i = 0; i<n; i++) {
            if(str.charAt(i) == 'c') {
                if(hash['k'] > 0) {
                    hash['k']--;
                }
                hash['c']++;
            }else {
                if(str.charAt(i) == 'r') {
                    if(hash['c'] == 0) return -1;
                    hash['c'] --;
                    hash['r'] ++;
                }else if(str.charAt(i) == 'o') {
                    if(hash['r'] == 0) return -1;
                    hash['r'] --;
                    hash['o'] ++;
                }else if(str.charAt(i) == 'a') {
                    if(hash['o'] == 0) return -1;
                    hash['o'] --;
                    hash['a'] ++;
                }else if(str.charAt(i) == 'k') {
                    if(hash['a'] == 0) return -1;
                    hash['a'] --;
                    hash['k'] ++;
                }
            }
        }
        for(int i = 0; i<4; i++) {
            if(hash[s[i]] != 0) return -1;
        }
        return hash['k'];
    }
}


 */